package com.example.demo.service;

import com.example.demo.entity.User;

import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    //注册
    public User register(String studentNumber,String password){
        //检查账号是否存在
        User existingUser = userMapper.findStudentNumber(studentNumber);
        if(existingUser==null){
            User newUser= new User();
            newUser.setStudentNumber(studentNumber);
            newUser.setPassword(password);
            userMapper.insert(studentNumber,password);
            return newUser;
        }
        else{
            throw new IllegalArgumentException("StudentNumber already exists.");
        }
    }

    public User login(String studentNumber,String password){
        if (userMapper.findStudentNumber(studentNumber)==null||userMapper.findPassword(password)==null){
            throw new IllegalArgumentException("StudentNumber or Password was wrong.");
        }
        User newUser= new User();
        newUser.setStudentNumber(studentNumber);
        newUser.setPassword(password);
        return newUser;
    }

    public User update(String studentNumber,String password){
        User existingUser = userMapper.findStudentNumber(studentNumber);
        if(existingUser!=null){
            userMapper.update(studentNumber,password);
            User newUser= userMapper.findStudentNumber(studentNumber);
            return newUser;
        }else {
            throw new IllegalArgumentException("StudentNumber already exists.");
        }
    }

    //获取管理员名字
    public String adminName(Integer id){
        return userMapper.adminName(id);
    }

}